import { ref, computed } from "vue";
import { defineStore } from "pinia";

export const useUserInfoStore = defineStore(
  "userInfo",
  () => {
    const openId = <any>ref(null);
    const token = <any>ref(null);
    const userInfo = <any>ref(null);
    const crowdInfo = <any>ref(null);

    // 更新openId
    function setOpenId(newVal: string) {
      openId.value = newVal;
    }

    // 更新token
    function setToken(newVal: string) {
      token.value = newVal;
    }

    // 更新userInfo
    function setUserInfo(newVal: string) {
      userInfo.value = newVal;
    }

    // 更新crowdInfo
    function setCrowdInfo(newVal: string) {
      crowdInfo.value = newVal;
    }

    // 退出登录
    function logout() {
      uni.removeStorageSync("userInfo");
      userInfo.value = null;
      crowdInfo.value = null;
      openId.value = null;
      token.value = null;
      uni.removeStorageSync("privacySatus");
      uni.reLaunch({
        url: "/pages/crowd/home/index",
      });
    }
    return {
      userInfo,
      openId,
      token,
      crowdInfo,
      setOpenId,
      setToken,
      setUserInfo,
      setCrowdInfo,
      logout,
    };
  },
  {
    persist: true,
  }
);
